package com.itheima.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.IDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <b><code>DishServiceImpl</code></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 24/06/2024 16:40.
 *
 * @author yanjr
 * @since reggie_take_out
 */
@Service("dishService")
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 菜品分页查询
     * @param page 当前页
     * @param pageSize 每页显示条数
     * @param name 菜品名称
     * @return 分页信息
     */
    @Override
    public R<Page<Dish>> queryDish(int page, int pageSize, String name) {
        Page<Dish> dishPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<Dish>().like(StrUtil.isNotEmpty(name), Dish::getName, name);
        return R.success(baseMapper.selectPage(dishPage, queryWrapper));
    }

    /**
     * 新增菜品
     * @param dishDto 菜品信息
     * @return 是否新增成功
     */
    @Override
    public R<String> addDish(DishDto dishDto) {
        // 新增菜品
        Dish dish = new Dish();
        BeanUtil.copyProperties(dishDto, dish);
        save(dish);
        // 新增口味
        if (CollectionUtil.isNotEmpty(dishDto.getFlavors())) {
            for (DishFlavor flavor : dishDto.getFlavors()) {
                flavor.setDishId(dish.getId());
                flavor.setId(null);
                dishFlavorMapper.insert(flavor);
            }
        }
        return R.success("添加成功");
    }

    /**
     * 根据id查询菜品信息
     * @param id 菜品id
     * @return 菜品信息
     */
    @Override
    public R<DishDto> queryDishById(Long id) {
        Dish dish = baseMapper.selectById(id);
        if (dish != null) {
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, id));
            DishDto dishDto = new DishDto();
            BeanUtil.copyProperties(dish, dishDto);
            dishDto.setFlavors(dishFlavors);
            return R.success(dishDto);
        }
        return null;
    }

    /**
     * 批量起售/停售
     * @param status 状态，起售-1，停售-0
     * @param ids 菜品id，多个以逗号分隔
     * @return 操作是否成功
     */
    @Override
    public R<String> changeStatus(Integer status, String ids) {
        String[] idArray = ids.split(",");
        ArrayList<Dish> dishes = new ArrayList<>();
        for (String s : idArray) {
            Dish dish = new Dish();
            dish.setId(new Long(s));
            dish.setStatus(status);
            dishes.add(dish);
        }
        this.updateBatchById(dishes);
        return R.success("操作成功");
    }

    /**
     * 批量删除
     * @param ids 菜品id，多个以逗号分隔
     * @return 操作是否成功
     */
    @Override
    public R<String> delteDishByIds(String ids) {
        ArrayList<String> idList = CollectionUtil.toList(ids.split(","));
        // 先删除口味
        dishFlavorMapper.delete(new LambdaQueryWrapper<DishFlavor>().in(DishFlavor::getDishId, idList));
        // 再删除菜品
        baseMapper.deleteBatchIds(idList);
        return R.success("删除成功");
    }

    /**
     * 修改菜品
     * @param dishDto 菜品信息
     * @return 操作是否成功
     */
    @Override
    public R<String> updateDish(DishDto dishDto) {
        // 删除菜品
        this.delteDishByIds(dishDto.getId().toString());
        // 新增菜品
        this.addDish(dishDto);
        return R.success("操作成功");
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId 分类id
     * @return 菜品信息
     */
    @Override
    public List<DishDto> queryByCategoryId(Long categoryId) {
        List<Dish> list = this.list(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, categoryId).eq(Dish::getStatus, 1));
        List<DishDto> result = list.stream().map(item -> {
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, item.getId()));
            DishDto dishDto = new DishDto();
            BeanUtil.copyProperties(item, dishDto);
            dishDto.setFlavors(dishFlavors);
            return dishDto;
        }).collect(Collectors.toList());
        return result;
    }
}
